/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LogFrame.java
 *
 * Created on 16-mars-2011, 12:58:45
 */
package com.google.code.p.narcissus.ui.components;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

import org.apache.log4j.Logger;

/**
 *
 * @author jadoj
 */
public class LogFrame extends javax.swing.JFrame {

    private static final Logger LOGGER = Logger.getLogger(LogFrame.class);
    private boolean updateLogPane = true;

    /** Creates new form LogFrame */
    public LogFrame() {
        initComponents();
    }

    public void start() {
        new Thread(
                new Runnable() {

                    @Override
                    @SuppressWarnings("SleepWhileHoldingLock")
                    public void run() {
                        try {
                            BufferedReader reader = new BufferedReader(new FileReader(new File("./Narcissus-ui.log")));
                            while (updateLogPane) {
                                String lineRead = reader.readLine();
                                if (lineRead != null) {
                                    while (lineRead != null) {
                                        addLineToLog(lineRead);
                                        lineRead = reader.readLine();
                                    }
                                } else {
                                    try {
                                        Thread.sleep(250);
                                    } catch (InterruptedException ex) {
                                        LOGGER.warn("Interrupted", ex);
                                    }
                                }
                            }
                        } catch (IOException fileNotReadableEx) {
                            LOGGER.error("Impossible to load log file!  Logs cannot be displayed on screen!", fileNotReadableEx);
                        }

                    }

                    private void addLineToLog(final String lineRead) {
                        java.awt.EventQueue.invokeLater(new Runnable() {

                            @Override
                            public void run() {
                                Document document = logPane.getDocument();
                                try {
                                    document.insertString(document.getLength(), lineRead + "\n", null);
                                } catch (BadLocationException ex) {
                                    LOGGER.error("impossible to write to LogPane!  No message can be displayed on screen", ex);
                                }
                            }
                        });
                    }
                }).start();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jScrollPane1 = new javax.swing.JScrollPane();
        logPane = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Logs");
        setMinimumSize(new java.awt.Dimension(800, 600));
        setName("LogFrame"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        getContentPane().setLayout(new java.awt.GridBagLayout());

        logPane.setEditable(false);
        jScrollPane1.setViewportView(logPane);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(jScrollPane1, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        this.updateLogPane = false;
    }//GEN-LAST:event_formWindowClosing

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new LogFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextPane logPane;
    // End of variables declaration//GEN-END:variables
}
